<?php

namespace app\admin\controller\report;

use app\common\controller\Backend;
use think\Config;

/**
 * 电厂报管理
 *
 * @icon fa fa-circle-o
 */
class Dcreport extends Backend
{

    /**
     * Dcreport模型对象
     * @var \app\admin\model\Dcreport
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\Dcreport;

    }

    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */


    public function index()
    {
        $begintime = strtotime('-2 month');

        $list = db('dcxiaohao')->where('riqi', 'gt', date('Y-m-d', $begintime))
            ->order('riqi asc')
            ->select();
        $rihao = $xieche = [];

        foreach ($list as $k => $v) {
            $day = $v['riqi'];
            $xieche[$day] = $v['xieche'];
            $rihao[$day] = $v['rihao'];
        }


        $this->view->assign([
            'paylist' => $rihao,
            'createlist' => $xieche,

        ]);

        return $this->view->fetch();
    }


    public function getnum()
    {

        if ($this->request->isAjax()) {
            $dc_id = input("dianchang", 0, 'intval');
            $map = [];
            if ($dc_id) {
                $map['dianchang_id'] = $dc_id;
            }

            $riqi = input("riqi");
            if ($riqi) {
                $b = explode(" - ", $riqi);
                if (count($b) != 2) $this->error("日期错误");
                if ($b) {
                    $map['riqi'] = ['between', $b];
                }
            }

            $list = db("dcxiaohao")
                ->field("dianchang_id,riqi,xieche,rihao,kucun,keyong")
                ->where($map)
                ->order("riqi asc")->select();
            //die(db()->getlastsql());
            // die($list);

            if(!$list)
                $this->error("没有找到时间段内数据");

            $riqi = [];
            $backdata=[];
            foreach ($list as $k => $v) {
                $triqi = strtotime($v['riqi']);
                $riqi[] = date("y/m/d", $triqi);
                $xieche[]=$v['xieche'];
                $rihao[]=$v['rihao'];
                $kucun[]=$v['kucun'];
                $keyong[]=$v['keyong'];
            }

            $backdata[]=['name'=>"卸车",'lstdata'=>$xieche];
            $backdata[]=['name'=>"日耗",'lstdata'=>$rihao];
            $backdata[]=['name'=>"库存",'lstdata'=>$kucun];
            $backdata[]=['name'=>"可用日期",'lstdata'=>$keyong];

            $res['riqi'] = $riqi;
            $res['title'] = $dc_id;
            $res['series'] = $backdata;

            //dump($price);die("87");
            //$list = collection($list)->toArray();

            /*
             name: __('Sales'),
                    type: 'line',
                    smooth: true,
                    areaStyle: {
                        normal: {}
                    },
                    lineStyle: {
                        normal: {
                            width: 1.5
                        }
                    },
                    data: Orderdata.paydata

             */

            return json($res);
        }

    }
}
